<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Hashids\Hashids;
use Illuminate\Support\Facades\DB;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    private $_data;

    private $_cityCode;

    public function __construct()
    {
        $this->_data['error_code'] = 0;
        $this->_data['data'] = (object)[];
        $this->_data['error_message'] = '';
    }

    public function response()
    {
        return response()->json($this->_data);
    }

    public function setStatus($status)
    {
        $this->_data['error_code'] = $status;
    }

    public function setMsg($errorMessage)
    {
        $this->_data['error_message'] = $errorMessage;
    }


    public function setData($data)
    {

        return is_array($data)? $this->_data['data']=$data:false;
    }

    public  function getToken()
    {
        return request()->header('X-auth-token', '');
    }

    public function getOrdId()
    {
        $hashids = new Hashids('', 10);

        return $hashids->decode(request()->header('X-org-code', ''))[0];
    }

    public function getPlatformId()
    {
        $hashids = new Hashids('', 10);

        return $hashids->decode(request()->header('X-platform-code', ''))[0];
    }

    public function getCityCode()
    {
        if($this->_cityCode > 0)
        {
            return $this->_cityCode;
        }

        $name =  urldecode(request()->header('X-city'));

        $city = DB::table('area')->where('level', 2)->where(function ($query) use ($name){
            $query->where('name', $name)->orwhere('short_name',$name);
        })->orderby('id')->first();

        $this->_cityCode = $city->id;

        return $this->_cityCode;
    }
}
